package com.vancouver.knows.ui.fragment.citydiscount;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.StateListDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscoutResultActivity;
import com.vancouver.knows.business.bean.DiscountCategoryInfo;

public class DiscountCategoryGridView extends LinearLayout {

	private LinearLayout discountLaout;
	private RadioGroup rindicatorLayout;
	private int next = 0;
	private AdPageAdapter pageAdapter;
	private ViewPager adViewPager;

	private final static int MAX_VALUE = 8;
	private final static int ROW_SHOW_COUNT = 4;
	private List<View> gridViewlist = new ArrayList<View>();
	private List<DiscountCategoryInfo> discountCategoryDatas = new ArrayList<>();

	public DiscountCategoryGridView(Context context) {
		super(context);
	}

	public DiscountCategoryGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void init(List<DiscountCategoryInfo> datas) {
		removeAllViews();
		int size = datas.size();
		if (size == 0) {
			return;
		}

		discountCategoryDatas = new ArrayList<>();
		gridViewlist = new ArrayList<View>();
		for (int i = 0; i < datas.size(); i++) {
			if (i != 0) {
				discountCategoryDatas.add(datas.get(i));
			}
		}

		setOrientation(LinearLayout.VERTICAL);
		LayoutInflater layoutInflater = LayoutInflater.from(getContext());
		View headView = layoutInflater.inflate(R.layout.discount_viewpage_gridview, this, false);
		headView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

		setView(headView);

		addView(headView);

//		setOrientation(LinearLayout.VERTICAL);
//		LayoutInflater layoutInflater = LayoutInflater.from(getContext());
//		LayoutParams params = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
//		int realSize = size - 1;// 第一个“全部商家”不显示
//		int row = (realSize % column > 0 ? 1 : 0) + realSize / column;
//		for (int i = 0; i < row; i++) {
//			LinearLayout lLayout = new LinearLayout(getContext());
//			lLayout.setOrientation(LinearLayout.HORIZONTAL);
//			for (int j = 0; j < column; j++) {
//				int index = i * column + j + 1;
//				View view;
//				if (index >= size) {
//					view = new View(getContext());
//				} else {
//					view = layoutInflater.inflate(R.layout.discount_category_item, lLayout, false);
//					bindData(view, datas.get(index), index);
//				}
//				lLayout.addView(view, params);
//			}
//			addView(lLayout);
//		}
	}

//	private void bindData(View view, final DiscountCategoryInfo discountCategoryInfo, final int position) {
//		TextView categoryName = (TextView) view.findViewById(R.id.categoryName);
//		ImageView categoryImg = (ImageView) view.findViewById(R.id.categoryImg);
//		categoryName.setText(discountCategoryInfo.getName());
//		if (!StringUtil.isEmptyString(discountCategoryInfo.getImageUrl())) {
//			Picasso.with(getContext()).load(discountCategoryInfo.getImageUrl())
//					.resizeDimen(R.dimen.discount_category_home_img, R.dimen.discount_category_home_img).into(categoryImg);
//		}
//		view.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				Intent intent = new Intent(getContext(), CityDiscoutResultActivity.class);
//				intent.putExtra("index", position);
//				getContext().startActivity(intent);
//			}
//		});
//	}

	private void setView(View view) {
		discountLaout = (LinearLayout) view.findViewById(R.id.view_pager_content);
		rindicatorLayout = (RadioGroup) view.findViewById(R.id.viewGroup);

		// 创建ViewPager
		adViewPager = new ViewPager(getContext());
		// 设置属性
		adViewPager.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
		discountLaout.addView(adViewPager);

		getGridView();
		initCirclePoint();

		// 将GridView添加到ViewPager显示
		adViewPager.setAdapter(pageAdapter);

		setGridViewListeners();
	}

	private void setGridViewListeners() {
		for (int i = 0; i < gridViewlist.size(); i++) {
			final GridView gridView = (GridView) gridViewlist.get(i);
			gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					DiscountCategoryInfo info = (DiscountCategoryInfo)parent.getItemAtPosition(position);
					for (int i = 0; i < discountCategoryDatas.size(); i++) {
						if (discountCategoryDatas.get(i).equals(info)){
							position = i + 1;
						}
					}

					Intent intent = new Intent(getContext(), CityDiscoutResultActivity.class);
					intent.putExtra("index", position);
					getContext().startActivity(intent);
				}
			});
		}
	}

	private void initCirclePoint() {
		int margin = getResources().getDimensionPixelSize(R.dimen.view_padding);
		int size = (int) getResources().getDimension(R.dimen.indicator_size);
		for (int i = 0; i < gridViewlist.size(); i++) {
			RadioButton indicator = new RadioButton(getContext());
			RadioGroup.LayoutParams radioParams = new RadioGroup.LayoutParams(size, size);
			radioParams.setMargins(margin, 0 , margin, 0);
			indicator.setLayoutParams(radioParams);
			indicator.setBackgroundResource(R.drawable.circle_indicator_selector);
			indicator.setButtonDrawable(new StateListDrawable());
			indicator.setText("");
			rindicatorLayout.addView(indicator);
		}

		RadioButton indicator = (RadioButton) rindicatorLayout.getChildAt(0);
		indicator.setChecked(true);

		adViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				RadioButton indicator = (RadioButton) rindicatorLayout.getChildAt(position);
				indicator.setChecked(true);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	private void getGridView() {
		boolean bool = true;
		next = 0;
		while (bool) {
			int result = next + MAX_VALUE;
			if (discountCategoryDatas.size() != 0 && result < discountCategoryDatas.size()) {
				GridView gridView = new GridView(getContext());
				gridView.setNumColumns(ROW_SHOW_COUNT);
				List<DiscountCategoryInfo> gridlist = new ArrayList<>();
				for (int i = next; i < result; i++) {
					gridlist.add(discountCategoryDatas.get(i));
				}
				MyAdapter myAdapter = new MyAdapter(gridlist);
				gridView.setAdapter(myAdapter);
				next = result;
				gridViewlist.add(gridView);

			} else if (result - discountCategoryDatas.size() <= MAX_VALUE) {
				List<DiscountCategoryInfo> gridlist = new ArrayList<>();
				for (int i = next; i < discountCategoryDatas.size(); i++) {
					gridlist.add(discountCategoryDatas.get(i));
				}
				GridView gridView = new GridView(getContext());
				gridView.setNumColumns(ROW_SHOW_COUNT);
				MyAdapter myAdapter = new MyAdapter(gridlist);
				gridView.setAdapter(myAdapter);
				next = discountCategoryDatas.size() - 1;
				gridViewlist.add(gridView);
				bool = false;
			} else {
				bool = false;
			}
		}
		pageAdapter = new AdPageAdapter(gridViewlist);

	}

	private class AdPageAdapter extends PagerAdapter {
		private List<View> views = null;

		public AdPageAdapter(List<View> views) {
			this.views = views;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(views.get(position));
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public Object instantiateItem(View container, final int position) {
			((ViewPager) container).addView(views.get(position), 0);

			return views.get(position);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
	}

	private class MyAdapter extends BaseAdapter {
		List<DiscountCategoryInfo> listgrid;

		private MyAdapter(List<DiscountCategoryInfo> listgrid) {
			this.listgrid = listgrid;
		}

		@Override
		public int getCount() {
			return listgrid.size();
		}

		@Override
		public DiscountCategoryInfo getItem(int position) {
			return listgrid.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			convertView = LayoutInflater.from(getContext()).inflate(R.layout.discount_category_item, null);
			TextView categoryName = (TextView) convertView.findViewById(R.id.categoryName);
			ImageView categoryImg = (ImageView) convertView.findViewById(R.id.categoryImg);

			categoryName.setText(listgrid.get(position).getName());
			if (!StringUtil.isEmptyString(listgrid.get(position).getImageUrl())) {
				Picasso.with(getContext()).load(listgrid.get(position).getImageUrl())
						.resizeDimen(R.dimen.discount_category_home_img, R.dimen.discount_category_home_img).into(categoryImg);
			}

			return convertView;
		}
	}
}
